<?php
/**
 * 
 * @author yuxun
 *
 */

class ModBossFall {
	
	public function select($params,$g_ids){
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getDriveDbName($platform,$params['g']));
	
		$start_ym = strtotime(date('Ym01 00:00:00',strtotime($params['time_start'])));
		$end_ym = strtotime(date('Ym01 00:00:00',strtotime($params['time_end'])));
// 		$start_ym = strtotime($params['time_start']);
// 		$end_ym = strtotime($params['time_end']);
		$monarr = array();
		$monarr[] = date('Ym',$start_ym);
		while( ($start_ym = strtotime('+1 month', $start_ym)) <= $end_ym){
			$monarr[] = date('Ym',$start_ym); // 取得递增月;
		}
	
		$where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period = :period AND GameSrc=:game ';
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => "Daily",
				':game'     => $params['g'],
		);
	
		if(!empty($params['BossId'])){
			$where .= " AND BossId LIKE :BossId ";
			$args['BossId'] = '%' . $params['BossId'] . '%';
		}
	
		if(!empty($params['ItemId'])){
			$where .= " AND ItemId LIKE :ItemId ";
			$args['ItemId'] = '%' . $params['ItemId'] . '%';
		}
	
	
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
	
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
	
		$where .= " AND ItemId IN({$g_ids}) ";
	
		$table  = "(";
		foreach ($monarr as $key=>$val){
			$oneTb = "tblgamebossfall_".$val;
			$table .= " (SELECT * FROM {$oneTb} {$where}) UNION ALL";
		}
		$table = substr($table, 0, -9);
		$table .= ') AS tbl';
	
		$order  = self::getOrder($params['order']);
		$fields = "BossId,ItemId,sum(ItemNum) as ItemNum";
		$sql = "SELECT {$fields} FROM {$table} {$where} group by BossId,ItemId {$order}";
	
		$data = $dbLink->query($sql, $args);
		return $data;
	}
	
	public function page($params,$g_ids){
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getDriveDbName($platform,$params['g']));

		$start_ym = strtotime(date('Ym01 00:00:00',strtotime($params['time_start'])));
		$end_ym = strtotime(date('Ym01 00:00:00',strtotime($params['time_end'])));
// 		$start_ym = strtotime($params['time_start']);
// 		$end_ym = strtotime($params['time_end']);
		$monarr = array();
		$monarr[] = date('Ym',$start_ym);
		while( ($start_ym = strtotime('+1 month', $start_ym)) <= $end_ym){
			$monarr[] = date('Ym',$start_ym); // 取得递增月;
		}		

		$where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period = :period AND GameSrc=:game ';
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => "Daily",
				':game'     => $params['g'],
		);
		
		if(!empty($params['BossId'])){
			$where .= " AND BossId LIKE :BossId ";
			$args['BossId'] = '%' . $params['BossId'] . '%';
		}
		
		if(!empty($params['ItemId'])){
			$where .= " AND ItemId LIKE :ItemId ";
			$args['ItemId'] = '%' . $params['ItemId'] . '%';
		}	

		
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
		
		$where .= " AND ItemId IN({$g_ids}) ";
		
		$table  = "(";
		foreach ($monarr as $key=>$val){
			$oneTb = "tblgamebossfall_".$val;
			$table .= " (SELECT * FROM {$oneTb} {$where}) UNION ALL";
		}
		$table = substr($table, 0, -9);
		$table .= ') AS tbl';
		
		
		$c_sql = "select count(1) as cnt from {$table} {$where} group by BossId,ItemId";
		$c_result = $dbLink->query($c_sql, $args);
		$count = count($c_result);
		//$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;
	
		$order  = self::getOrder($params['order']);
		$limit  = PubFunc::getLimit($params['length'], $params['page']);
		$fields = "BossId,ItemId,sum(ItemNum) as ItemNum";
		$sql = "SELECT {$fields} FROM {$table} {$where} group by BossId,ItemId {$order} {$limit}";

		$data = $dbLink->query($sql, $args);
		$result = array('count'=>$count,'data'=>$data);
		return $result;
	}

	public static function getOrder($order){
		if(PubFunc::checkEmpty($order, array(0, 1))) return '';
		if(!in_array($order[1], array('asc', 'desc'))){
			$sort = 'asc';
		}else{
			$sort = $order[1];
		}
		return " ORDER BY {$order[0]} {$sort} ";
	}
	
	
	public static function getInfo($params,$b_id_array){
		if(count($b_id_array) == 0){
			return array();
		}
		$b_ids = implode(',', $b_id_array);
		$b_ids = trim($b_ids,',');
		
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getDriveDbName($platform,$params['g']));
		
		$start_ym = strtotime($params['time_start']);
		$end_ym = strtotime($params['time_end']);
		$monarr = array();
		$monarr[] = date('Ym',$start_ym);
		while( ($start_ym = strtotime('+1 month', $start_ym)) <= $end_ym){
			$monarr[] = date('Ym',$start_ym); // 取得递增月;
		}
		
		
		$where  = ' WHERE RecDate BETWEEN :begin AND :end AND Period = :period AND GameSrc=:game ';
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => "Daily",
				':game'     => $params['g'],
		);
		
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
		
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
		
		$where .= " AND BossId IN({$b_ids}) ";
		
		$table  = "(";
		foreach ($monarr as $key=>$val){
			$oneTb = "tblgamebossdeath_".$val;
			$table .= " (SELECT * FROM {$oneTb} {$where}) UNION ALL";
		}
		$table = substr($table, 0, -9);
		$table .= ') AS tbl';
		
		$fields = "BossId,SUM(KillAll) as KillAll";
		$sql = "SELECT {$fields} FROM {$table} {$where} group by BossId ";

		$data = $dbLink->query($sql, $args);
		return $data;
	}

    
}